 
package c.x.jy.service.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import c.x.jy.common.utils.Constant;
import c.x.jy.dao.dao.SysUserDao;
import c.x.jy.dao.entity.SysUserEntity;
import c.x.jy.service.redis.Cache;
import c.x.jy.service.service.ShiroService;
@CacheConfig(cacheNames = {"user"})
@Service
public class ShiroServiceImpl implements ShiroService {
 
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private   Cache redisUtils;

  //  @Cacheable(value="permission",key = "targetClass + methodName +#p0")
    @Override
    public Set<String> getUserPermissions(Long userId) {
        List<String> permsList;
        Set<String> permsSet =   (Set<String>) redisUtils.get("user:permission:getUserPermissions:"+userId,  Set.class);
        if(permsSet==null) {
            SysUserEntity user = sysUserDao.selectById(userId);
        //系统管理员，拥有最高权限
        if(user.getBusinessId() == Constant.SUPER_ADMIN){

            permsList = sysUserDao.queryAllPerms(null);
        }else{
            permsList = sysUserDao.queryAllPerms(userId);
        }
      //  System.out.println("权限："+JSON.toJSONString(permsList));
        //用户权限列表
      permsSet = new HashSet<>();
        for(String perms : permsList){
            if(StringUtils.isBlank(perms)){
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        if(permsSet.size()>0) {
        	redisUtils.set("user:permission:getUserPermissions:"+userId,  permsSet,120l);
        }
        
        }
        return permsSet;
    }

    @Override
    public SysUserEntity  queryByToken(String token) {
 
      return	(SysUserEntity ) redisUtils.get(token, SysUserEntity .class);
    }
  
}
